Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0, require("./wx-cloud");

var _config = _interopRequireDefault(require("./config")), _util = _interopRequireDefault(require("../public/utils/util")), _user = _interopRequireDefault(require("../public/utils/user"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _possibleConstructorReturn(e, t) {
    return !t || "object" !== _typeof(t) && "function" != typeof t ? _assertThisInitialized(e) : t;
}

function _assertThisInitialized(e) {
    if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    return e;
}

function _getPrototypeOf(e) {
    return (_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(e) {
        return e.__proto__ || Object.getPrototypeOf(e);
    })(e);
}

function _inherits(e, t) {
    if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function");
    e.prototype = Object.create(t && t.prototype, {
        constructor: {
            value: e,
            writable: !0,
            configurable: !0
        }
    }), t && _setPrototypeOf(e, t);
}

function _setPrototypeOf(e, t) {
    return (_setPrototypeOf = Object.setPrototypeOf || function(e, t) {
        return e.__proto__ = t, e;
    })(e, t);
}

function _typeof(e) {
    return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
        return typeof e;
    } : function(e) {
        return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
    })(e);
}

function _classCallCheck(e, t) {
    if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function");
}

function _defineProperties(e, t) {
    for (var r = 0; r < t.length; r++) {
        var o = t[r];
        o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), 
        Object.defineProperty(e, o.key, o);
    }
}

function _createClass(e, t, r) {
    return t && _defineProperties(e.prototype, t), r && _defineProperties(e, r), e;
}

var Report = function() {
    function e() {
        var o = this;
        _classCallCheck(this, e);
        this.config = {
            repeat: 1,
            except: [ /Expect END descriptor with depth \d but get another/, function(e) {
                var t = e.msg, r = void 0 === t ? "" : t, o = e.e, n = void 0 === o ? {} : o;
                return !!(/"isdefault":1/.test(r) && (-11 === n.code || -1 === n.code) || /market\/corp\/join/.test(r) && -5 === n.code || /minicard\/auth\/getapplydetail/.test(r) && -11 === n.code || /v4\/weappcard\/createv2/.test(r) && -170001 === n.code || /minicard\/card\/viewbyuser/.test(r) && -11 === n.code);
            } ]
        }, this.errorQueue = [], this.repeatList = {}, [ "error" ].forEach(function(t, r) {
            o[t] = function(e) {
                return o.handleMsg(e, t, r);
            };
        });
    }
    return _createClass(e, [ {
        key: "repeat",
        value: function(e) {
            var t = e.msg;
            return this.repeatList[t] = this.repeatList[t] ? this.repeatList[t] + 1 : 1, this.repeatList[t] > this.config.repeat;
        }
    }, {
        key: "except",
        value: function(e) {
            for (var t = this.config.except, r = 0; r < t.length; r++) {
                if ("function" == typeof t[r] ? t[r](e) : t[r].test(e.msg)) return !0;
            }
            return !1;
        }
    }, {
        key: "catchError",
        value: function(e) {
            return !this.except(e) && (!this.repeat(e) && (this.errorQueue.push(e), this.errorQueue));
        }
    }, {
        key: "handleMsg",
        value: function(e, t, r) {
            if (e) {
                var o = "object" === _typeof(e) ? e : {
                    msg: e
                };
                return o.type = t, o.level = r, this.catchError(o) && this.send(o), o;
            }
        }
    }, {
        key: "send",
        value: function() {
            var e = {
                uid: _user.default.getTokensSync().uid,
                error: this.errorQueue[0],
                version: _config.default.appVersion,
                systemInfo: this.systemInfo,
                breadcrumbs: this.breadcrumbs,
                time: new Date().toLocaleString()
            };
            if (this.errorQueue = [], this.isDev) return console.log(JSON.stringify(e, null, 2));
            this.request(e);
        }
    }, {
        key: "request",
        value: function(e) {
            wx.cloud.callFunction({
                data: e,
                name: "sendEmail",
                success: function(e) {
                    console.log(e);
                },
                fail: console.error
            });
        }
    } ]), e;
}(), Debug = function(e) {
    function r() {
        var e;
        _classCallCheck(this, r), (e = _possibleConstructorReturn(this, _getPrototypeOf(r).call(this))).breadcrumbs = [];
        var t = wx.getSystemInfoSync();
        return e.isDev = "devtools" === t.platform, e.systemInfo = JSON.stringify(t), e.rewriteApp(), 
        e.rewritePage(), e.recordComponent(), e;
    }
    return _inherits(r, Report), _createClass(r, [ {
        key: "rewriteApp",
        value: function() {
            var n = this, t = App;
            App = function(e) {
                return [ "onLaunch", "onShow", "onHide", "onError" ].forEach(function(r) {
                    var o = e[r];
                    e[r] = function(e) {
                        var t = {
                            belong: "App",
                            method: r,
                            path: e && e.path,
                            query: Object.keys(e && e.query || {}).length ? _util.default.serialize(e.query) : "",
                            scene: e && e.scene
                        };
                        return "onError" === r ? n.error({
                            msg: e
                        }) : n.pushToBreadcrumb(t), o && o.call(this, e);
                    };
                }), t(e);
            };
        }
    }, {
        key: "rewritePage",
        value: function() {
            var r = this, e = Page;
            Page = function(t) {
                return Object.keys(t).forEach(function(e) {
                    "function" == typeof t[e] && r.recordPageFn(t, e);
                }), t.onReady || r.recordPageFn(t, "onReady"), t.onLoad || r.recordPageFn(t, "onLoad"), 
                e(t);
            };
        }
    }, {
        key: "recordPageFn",
        value: function(e, i) {
            if (!this.methodFilter(i)) {
                var u = this, s = e[i];
                e[i] = function() {
                    for (var e = {
                        method: i,
                        belong: "Page",
                        route: this.route,
                        query: _util.default.serialize(this.__query || {})
                    }, t = arguments.length, r = new Array(t), o = 0; o < t; o++) r[o] = arguments[o];
                    var n = r[0];
                    return n && (n.detail || n.touches || n.target) && (e.arg = n), u.pushToBreadcrumb(e), 
                    s && s.apply(this, r);
                };
            }
        }
    }, {
        key: "methodFilter",
        value: function(e) {
            return "onPageScroll" === e;
        }
    }, {
        key: "recordComponent",
        value: function() {
            var r = this, o = Component;
            Component = function(e) {
                var t = e.methods || {};
                return Object.keys(t).forEach(function(e) {
                    "function" == typeof t[e] && r.recordComponentFn(t, e);
                }), o(e);
            };
        }
    }, {
        key: "recordComponentFn",
        value: function(e, n) {
            var i = this, u = e[n];
            e[n] = function() {
                var e = {
                    method: n,
                    belong: "Component"
                };
                i.pushToBreadcrumb(e);
                for (var t = arguments.length, r = new Array(t), o = 0; o < t; o++) r[o] = arguments[o];
                return u && u.apply(this, r);
            };
        }
    }, {
        key: "pushToBreadcrumb",
        value: function(e) {
            var t = this.breadcrumbs;
            t.push(e), 30 < t.length && t.shift();
        }
    } ]), r;
}(), debug = new Debug(), _default = debug;

exports.default = _default;